from knowledge_base_directory import knowledge_base_directory
from knowledge_base_directory import phrase_file_extension
from knowledge_base_directory import phrase_file_creation_time
from knowledge_base_directory import phrase_file_modification_time
from knowledge_base_directory import phrase_file_size

class mediator_fake :
    def __init__ ( self ) :
        self . _directory = None
        self . _current_time = None
        self . _created_files = [ ]
        self . _deleted_files = [ ]
        self . _list_files = [ ]
        self . _read_files = { }
        self . _call_history = [ ]
        self . _knowledge_base_creation_time_reply = None
        self . _knowledge_base_modification_time_reply = None
        self . _knowledge_base_size_reply = None
        self . _read_file_request = None
        self . _list_knowledge_bases_reply = None
    def current_time_request ( self ) :
        self . _directory . current_time_reply ( self . _current_time )
    def create_file ( self , name , contents ) :
        self . _created_files += [ [ name , contents ] ]
    def delete_file ( self , name ) :
        self . _call_history += [ mediator_fake . delete_file ]
        self . _deleted_files += [ name ]
    def list_files_request ( self ) :
        self . _directory . list_files_reply ( self . _list_files )
    def read_file_request ( self , name ) :
        self . _call_history += [ mediator_fake . read_file_request ]
        self . _directory . read_file_reply ( name , self . _read_files [ name ] )
    def knowledge_base_creation_time_reply ( self , time ) :
        self . _knowledge_base_creation_time_reply = time
    def knowledge_base_modification_time_reply ( self , time ) :
        self . _knowledge_base_modification_time_reply = time
    def knowledge_base_size_reply ( self , size ) :
        self . _knowledge_base_size_reply = size
    def list_knowledge_bases_reply ( self , bases ) :
        self . _list_knowledge_bases_reply = bases

class knowledge_base_directory_direct_call_tests :
    'knowledge_base_directory_direct_call'
    def __init__ ( self , test ) :
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _directory = knowledge_base_directory ( self . _mediator )
        self . _mediator . _directory = self . _directory
    def test_create_knowledge_base ( self ) :
        self . _mediator . _current_time = 'my time'
        self . _directory . create_knowledge_base ( 'my base' )
        self . _test . assume_equal ( self . _mediator . _created_files , [ [ 'my base' + phrase_file_extension ( ) , 
            [ phrase_file_creation_time ( ) + 'my time' 
            , phrase_file_modification_time ( ) + 'my time'
            , phrase_file_size ( ) + '0'
            ] ] ] )
    def test_rename_knowledge_base ( self ) :
        self . _mediator . _current_time = 'my current time'
        self . _mediator . _read_files = { 'my base' + phrase_file_extension ( ) : 
            [ phrase_file_creation_time ( ) + 'my creation time'
            , phrase_file_modification_time ( ) + 'my modification time'
            , phrase_file_size ( ) + 'my size'
            ] }
        self . _directory . rename_knowledge_base ( 'my base' , 'my new base' )
        self . _test . assume_equal ( self . _mediator . _created_files , [ [ 'my new base' + phrase_file_extension ( ) , 
            [ phrase_file_creation_time ( ) + 'my creation time' 
            , phrase_file_modification_time ( ) + 'my current time'
            , phrase_file_size ( ) + 'my size'
            ] ] ] )
        self . _test . assume_equal ( self . _mediator . _deleted_files , [ 'my base' + phrase_file_extension ( ) ] )
        self . _test . assume_equal ( self . _mediator . _call_history [ - 1 ] , mediator_fake . delete_file )
    def test_rename_knowledge_base_to_same_name ( self ) :
        self . _mediator . _current_time = 'my current time'
        self . _mediator . _read_files = { 'my base' + phrase_file_extension ( ) : 
            [ phrase_file_creation_time ( ) + 'my creation time'
            , phrase_file_modification_time ( ) + 'my modification time'
            , phrase_file_size ( ) + 'my size'
            ] }
        self . _directory . rename_knowledge_base ( 'my base' , 'my base' )
        self . _test . assume_empty ( self . _mediator . _created_files )
        self . _test . assume_empty ( self . _mediator . _deleted_files )
    def test_copy_knowledge_base ( self ) :
        self . _mediator . _current_time = 'my current time'
        self . _mediator . _read_files = { 'my base' + phrase_file_extension ( ) : 
            [ phrase_file_creation_time ( ) + 'my creation time'
            , phrase_file_modification_time ( ) + 'my modification time'
            , phrase_file_size ( ) + 'my size'
            ] }
        self . _directory . copy_knowledge_base ( 'my base' , 'my new base' )
        self . _test . assume_equal ( self . _mediator . _created_files , [ [ 'my new base' + phrase_file_extension ( ) , 
            [ phrase_file_creation_time ( ) + 'my creation time' 
            , phrase_file_modification_time ( ) + 'my current time'
            , phrase_file_size ( ) + 'my size'
            ] ] ] )
    def test_list_knowledge_bases ( self ) :
        self . _mediator . _list_files = \
            [ 'my first arbitrary file' 
            , 'my second arbitrary file' 
            , 'my first knowledge base' + phrase_file_extension ( )
            , 'my second knowledge base' + phrase_file_extension ( )
            ]
        self . _directory . list_knowledge_bases_request ( )
        self . _test . assume_equal ( self . _mediator . _list_knowledge_bases_reply ,
            [ 'my first knowledge base'
            , 'my second knowledge base'
            ] )
    def test_knowledge_base_creation_time ( self ) :
        self . _mediator . _read_files = { 'my base' + phrase_file_extension ( ) : 
            [ phrase_file_creation_time ( ) + 'my creation time' 
            ] }
        self . _directory . knowledge_base_creation_time_request ( 'my base' )
        self . _test . assume_equal ( self . _mediator . _knowledge_base_creation_time_reply , 'my creation time' )
    def test_knowledge_base_modification_time ( self ) :
        self . _mediator . _read_files = { 'my base' + phrase_file_extension ( ) : 
            [ ''
            , phrase_file_modification_time ( ) + 'my modification time' 
            ] }
        self . _directory . knowledge_base_modification_time_request ( 'my base' )
        self . _test . assume_equal ( self . _mediator . _knowledge_base_modification_time_reply , 'my modification time' )
    def test_knowledge_base_size ( self ) :
        self . _mediator . _read_files = { 'my base' + phrase_file_extension ( ) : 
            [ ''
            , ''
            , phrase_file_size ( ) + 'my size' 
            ] }
        self . _directory . knowledge_base_size_request ( 'my base' )
        self . _test . assume_equal ( self . _mediator . _knowledge_base_size_reply , 'my size' )
